package com.shakev.coupon.es.service.impl;

import com.shakev.coupon.es.model.Mark;
import com.shakev.coupon.es.repository.MarkRepository;
import com.shakev.coupon.es.service.EMarkService;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @ClassName EMarkServiceImpl
 * @Description TODO
 * @Author 李伟松
 * @CreateDate 2019-06-11 下午 10:49
 * @UpdateDate 2019-06-11 下午 10:49
 * @Version 1.0
 */
@Service
public class EMarkServiceImpl implements EMarkService {

    @Resource
    MarkRepository markRepository;

    @Override
    public long count() {
        return 0;
    }

    @Override
    public Mark findById(String id) {
        return null;
    }

    @Override
    public Mark findByMark(Mark mark){
        if(mark!=null){
            String id = "";
            if(!StringUtils.isEmpty(mark.getId())){
                id = mark.getId();
            }else if (!StringUtils.isEmpty(mark.getUserId()) && !StringUtils.isEmpty(mark.getItemId())){
                id = getMarkId(mark);
            }
            Optional<Mark> res = markRepository.findById(id);
            if(res!=null && !res.equals(Optional.empty()) ){
                return res.get();
            }
        }
        return null;
    }

    @Override
    public String getMarkId(Mark mark){
        return  mark.getItemId()+"10abc01"+mark.getUserId();
    }

    @Override
    public Mark save(Mark mark) {
        if(mark!=null){
            String id = getMarkId(mark);
            mark.setId(id);
            mark.setCreateTime(new Date().getTime());
            return markRepository.save(mark);
        }
        return null;

    }

    @Override
    public void delete(Mark mark) {
        String id = "";
        if(!StringUtils.isEmpty(mark.getId())){
            id = mark.getId();
        }else if (!StringUtils.isEmpty(mark.getUserId()) && !StringUtils.isEmpty(mark.getItemId())){
            id = getMarkId(mark);
        }
        deleteById(id);
    }

    @Override
    public void deleteById(String id) {
        markRepository.deleteById(id);
    }

    @Override
    public Iterable<Mark> getAll() {
        return null;
    }

    @Override
    public List<String> queryItemIdsByUserId(Integer pageNo, Integer pageSize, String userId) {

        List<String> itemIds = new ArrayList<>();
        QueryBuilder matchQueryBuilder = QueryBuilders.termQuery("userId", userId);
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withFilter(matchQueryBuilder)
                .withSort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC))
                .withPageable(PageRequest.of(pageNo, pageSize))
                .build();
        Page<Mark> page = markRepository.search(searchQuery);
        if(page!=null && page.getContent()!=null && page.getContent().size()>0){
            page.getContent().forEach(e ->itemIds.add(e.getItemId()));
        }
        return itemIds;
    }

    //    @PostConstruct
    void v() {
        Mark mark1 = new Mark();
        mark1.setId("58881324152210abc01"+"1");
        mark1.setItemId("588813241522");
        mark1.setUserId("1");
        mark1.setCreateTime(System.currentTimeMillis());
        save(mark1);
        Mark mark2 = new Mark();
        mark2.setId("58811720121710abc011");
        mark2.setItemId("588117201217");
        mark2.setUserId("1");
        mark2.setCreateTime(System.currentTimeMillis());
        save(mark2);
        Mark mark3 = new Mark();
        mark3.setId("59102873690710abc011");
        mark3.setUserId("1");
        mark3.setItemId("591028736907");
        mark3.setCreateTime(System.currentTimeMillis());
        save(mark3);

//        List<String> res = queryItemIdsByUserId("1");
//        if (res != null && res.size() > 0) {
//            System.out.println("查询结果：");
//            for (String re : res) {
//                System.out.println(re);
//            }
//        }


    }

    @Override
    public List<Mark> findByName(String name) {
        return null;
    }

    @Override
    public Page<Mark> pageQuery(Integer pageNo, Integer pageSize, String kw) {
        return null;
    }
}
